package com.wangyadong.hobby.common.utils.email;


import com.wangyadong.hobby.common.utils.ConfigurableApplitionUtil;
import com.wangyadong.hobby.front.model.WemailRecord;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * @ClassName SendEmailEvent
 * @Description TODO
 * @Authpr Antonio
 * @Data 2018-07-14 11:49
 */
public class SendEmailEvent implements Runnable {
    private static Logger LOGGER = LoggerFactory.getLogger(SendEmailEvent.class);
    private JavaMailSender mailSender;
    private String email;
    private String title;
    private String fromMail;
    private String userName;
    private Long id;
    private FreeMarkerConfigurer configurer;


    public SendEmailEvent(JavaMailSender mailSender, WemailRecord wemailRecord, String title, FreeMarkerConfigurer configurer) {
        this.userName = wemailRecord.getUserName();
        this.email = wemailRecord.getEmail();
        this.title = title;
        this.mailSender = mailSender;
        this.fromMail = wemailRecord.getFromEmail();
        this.configurer = configurer;
        this.id = wemailRecord.getId();
    }

    @Override
    public void run() {
        int count = 0;
        final MimeMessage mimeMessage = mailSender.createMimeMessage();
        final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
        Map<String, Object> model = new HashMap<>();
        model.put("toUserName", userName);
        model.put("time", new Date());
        model.put("link", ConfigurableApplitionUtil.getProperty("hostname") + "/wuserInfo/vaildateFindEmail?id=" + id);
        while (count <= 2) {
            try {
                String parse = parse(model);
                if ("-1".equals(parse)) {
                    break;
                }
                message.setFrom(fromMail);
                message.setTo(email);
                message.setSubject(title);
                message.setText(parse, true);
                mailSender.send(mimeMessage);
                break;
            } catch (Exception ex) {
                LOGGER.error("邮件发送失败", ex);
                count++;
            }
        }
        mailSender = null;
        configurer = null;
    }

    private String parse(Map model) {
        String s = null;
        try {
            Template template = null;
            Configuration configuration = configurer.getConfiguration();
            template = configuration.getTemplate("email.ftl");
            s = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
        } catch (IOException e) {
            LOGGER.error("模板文件解析出错", e);
            s = "-1";
        } catch (TemplateException e) {
            LOGGER.error("模板文件没有找到", e);
            s = "-1";
        }
        return s;
    }


}
